package com.bj.disc.ui.splash.fragment

import android.os.Bundle
import android.view.View
import com.ai3d.rfid.base.app.App.Companion.appViewModel
import com.ai3d.rfid.base.fragment.BaseVmVdbFragment
import com.ai3d.rfid.common.event.EventMgs
import com.ai3d.rfid.common.log.Logcat
import com.ai3d.rfid.common.proxy.MMKVProxy
import com.ai3d.rfid.common.proxy.MMKVProxy.KEY_NEED_DOWNLOAD_RANGE
import com.ai3d.rfid.common.proxy.MMKVProxy.KEY_NEED_NEED_UNZIP
import com.ai3d.rfid.common.utils.HandlerUtils
import com.ai3d.rfid.databinding.FragmentSplashBinding
import com.ai3d.rfid.network.download.IDownloadListener
import com.bj.disc.activity.MainActivity
import com.bj.disc.ui.splash.vm.SplashViewModel


/**
 * @ProjectName : Kolb
 * @Author : weijinsong
 * @Time : 2022/12/30 14:37
 * @Description : 启动页面
 */
class SplashFragment : BaseVmVdbFragment<SplashViewModel, FragmentSplashBinding>() {
    companion object {
        private const val TAG = "SplashFragment"
        var INIT_SDK_KEY = "init"

    }
    private var count=0

    override fun initView(savedInstanceState: Bundle?) {
        mViewModel.initStepList()
    }

    override fun createObserver() {
        mViewModel.viewStatusMLD.observe(this) {
        }

        appViewModel.failure.observe(this) {
            gotoLogin()
        }
        mViewModel.doNextStep()
    }


    override fun onResume() {
        super.onResume()
        mViewModel.onResume()
    }

    override fun onPause() {
        super.onPause()
        mViewModel.onPause()
    }

    /**
     * 显示隐私弹窗
     */

    private fun gotoUserAgreement() {
//        activity?.let { H5Activity.startH5Activity(it, USER_AGREEMENT_PATH) }
    }

    private fun gotoPrivacyPolicy() {
//        activity?.let { H5Activity.startH5Activity(it, PRIVACY_POLICY_PATH) }
    }

    private fun gotoLogin() {
    }

    private fun gotoMain() {
        EventMgs.insertFailureData()
        MainActivity.startMainActivity(requireActivity())
    }

    private var isDownloading = false
    private fun downloadStaticFile() {
        mViewModel.downloadStaticFile(object : IDownloadListener {
            override fun onStartDownload(length: Long) {
                if (activity?.isFinishing == false && activity?.isDestroyed == false) {
                    isDownloading = true
                    Logcat.d(TAG, "onStartDownload length : $length")
                }
            }

            override fun onProgress(progress: Double) {
                if (activity?.isFinishing == false && activity?.isDestroyed == false) {
                    //onStartDownload调用不准确，故移到这里
                    mDatabind.progressBar.visibility = View.VISIBLE
                    mDatabind.tvPbTips.visibility = View.VISIBLE
                    mDatabind.tvPb.visibility = View.VISIBLE
//                    Logcat.d(TAG, "onProgress progress : $progress")
                    mDatabind.progressBar?.progress = progress.toInt()
                }
            }

            override fun onComplete(length: Long) {
                if (isDownloading) {
                    if (activity?.isFinishing == false && activity?.isDestroyed == false) {
                        MMKVProxy.setBoolean(KEY_NEED_DOWNLOAD_RANGE, false)
                        MMKVProxy.setBoolean(KEY_NEED_NEED_UNZIP, true)
                        Logcat.d(TAG, "onComplete")
                        mViewModel.doNextStep()
                        Logcat.d("SplashViewModel", "onComplete doNextStep")
                    }
                }
                isDownloading = false
            }

            override fun onFail(errorInfo: String?) {
                if (activity?.isFinishing == false && activity?.isDestroyed == false) {
                    mDatabind.progressBar.visibility = View.GONE
                    mDatabind.tvPbTips.visibility = View.GONE
                    mDatabind.tvPb.visibility = View.GONE
                    Logcat.d(TAG, "onFail errorInfo : $errorInfo")
                    HandlerUtils.removeCallbacksAndMessages()
                        HandlerUtils.postDelayed({
                            downloadStaticFile()
                        },3*1000)
                    //mViewModel.doNextStep()
                    Logcat.d("SplashViewModel", "onFail doNextStep")
                }
            }

        })
    }

}